package com.hxd.mall.utils.open

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.hxd.mall.constant.HxdGlobalInfo

/**
 * Create by yinzhengwei on 2019/4/25
 * @Function 接收搜集的风控数据(电量)
 *
 * 此功能只要应用不退出，则会一直在刷新deviceBattery，系统电量只要一变化都会收到此广播
 */
class BatteryReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        //判断它是否是为电量变化的Broadcast Action
        if (Intent.ACTION_BATTERY_CHANGED == intent?.action) {
            //获取当前电量
            val level = intent.getIntExtra("level", 0)
            //电量的总刻度
            val scale = intent.getIntExtra("scale", 100)
            HxdGlobalInfo.deviceBattery = level * 100 / scale
            Log.d("deviceBattery", HxdGlobalInfo.deviceBattery.toString())
        }
    }
}